import pojo.USER;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/test")
public class HelloServlet extends HttpServlet {
    private static String message = new String("Hello servlet!");

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.println("<h1>" + message + "</h1>");
        //super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        USER prepare_user = new USER("root", "123");
        String msg = new String();
        // get informations
        String username = req.getParameter("name");// index.jsp: name="name"
        String password = req.getParameter("pass");
        // logical judgement
        if (prepare_user.getUsername().equals(username) && prepare_user.getPassword().equals(password)) {
            resp.sendRedirect("success.jsp");
        } else {
            msg = "username=" + prepare_user.getUsername() + ",password=" + prepare_user.getPassword();
            // print
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().write("<h1>" + msg + "</h1>");
        }
        //super.doPost(req, resp);
    }
}
